package com.shop.zkmall.app.service.a.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.zkmall.app.dao.buyer.CereBuyerUserDAO;
import com.shop.zkmall.app.domain.CereBuyerUserFollow;
import com.shop.zkmall.app.domain.res.AttentionRes;
import com.shop.zkmall.app.mapper.CereBuyerUserFollowMapper;
import com.shop.zkmall.app.service.a.ICereBuyerUserFollowService;
import com.shop.zkmall.app.utils.LoginHelper;
import com.shop.zkmall.app.utils.R;
import com.shop.zkmall.commons.domain.buyer.CereBuyerUser;
import com.shop.zkmall.commons.domain.common.PageParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class CereBuyerUserFollowServiceImpl extends ServiceImpl<CereBuyerUserFollowMapper, CereBuyerUserFollow> implements ICereBuyerUserFollowService {

    @Autowired
    private CereBuyerUserDAO cereBuyerUserDAO;


    @Override
    public R<List<AttentionRes>> attentionList(PageParam param) {
        Long userId = LoginHelper.getUserId();

        List<AttentionRes> attentionRes = new ArrayList<>();
        Page<CereBuyerUserFollow> page = Page.of(param.getPage(), param.getPageSize());
        Page<CereBuyerUserFollow> page1 = lambdaQuery().eq(CereBuyerUserFollow::getUserId, userId).page(page);
        if (CollUtil.isEmpty(page1.getRecords())){
            return R.ok(new ArrayList<>());
        }
        List<Long> list = page1.getRecords().stream().map(CereBuyerUserFollow::getFollowId).toList();
        LambdaQueryWrapper<CereBuyerUser> cereBuyerUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
        cereBuyerUserLambdaQueryWrapper.in(CereBuyerUser::getBuyerUserId, list);
        List<CereBuyerUser> cereBuyerUsers = cereBuyerUserDAO.selectList(cereBuyerUserLambdaQueryWrapper);

        for (CereBuyerUserFollow record : page1.getRecords()) {
            AttentionRes res = new AttentionRes();
            res.setUserId(record.getFollowId());
            cereBuyerUsers.stream().findFirst().ifPresent(cereBuyerUser -> {
                res.setNickName(cereBuyerUser.getName());
                res.setHeadImage(cereBuyerUser.getHeadImage());
            });
            attentionRes.add(res);
        }


        return R.ok(attentionRes);
    }

    @Override
    public R<String> addOrCancel(Long targetId, Integer type) {
        Long userId = LoginHelper.getUserId();
        if (type == 0){
            CereBuyerUserFollow cereBuyerUserFollow = new CereBuyerUserFollow();
            cereBuyerUserFollow.setUserId(userId);
            cereBuyerUserFollow.setFollowId(targetId);
            //查询是否已关注
            CereBuyerUserFollow one = lambdaQuery().eq(CereBuyerUserFollow::getUserId, userId).eq(CereBuyerUserFollow::getFollowId, targetId).one();
            if (one != null){
                //已关注
                return R.fail("已关注");
            }
            save(cereBuyerUserFollow);
        }
        if (type == 1){
            //取消关注
            remove(new LambdaQueryWrapper<CereBuyerUserFollow>().eq(CereBuyerUserFollow::getUserId, userId).eq(CereBuyerUserFollow::getFollowId, targetId));
        }
        return R.ok();
    }

    @Override
    public R<List<AttentionRes>> fanList(PageParam param) {
        Long userId = LoginHelper.getUserId();

        List<AttentionRes> attentionRes = new ArrayList<>();
        Page<CereBuyerUserFollow> page = Page.of(param.getPage(), param.getPageSize());
        Page<CereBuyerUserFollow> page1 = lambdaQuery().eq(CereBuyerUserFollow::getFollowId, userId).page(page);
        if (CollUtil.isEmpty(page1.getRecords())){
            return R.ok(new ArrayList<>());
        }

        //查询我关注的用户
        List<CereBuyerUserFollow> My = lambdaQuery().eq(CereBuyerUserFollow::getUserId, userId).list();
        //收集成关注用户列表id
        List<Long> followIdList = My.stream().map(CereBuyerUserFollow::getFollowId).toList();

        List<Long> list = page1.getRecords().stream().map(CereBuyerUserFollow::getUserId).toList();
        LambdaQueryWrapper<CereBuyerUser> cereBuyerUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
        cereBuyerUserLambdaQueryWrapper.in(CereBuyerUser::getBuyerUserId, list);
        List<CereBuyerUser> cereBuyerUsers = cereBuyerUserDAO.selectList(cereBuyerUserLambdaQueryWrapper);

        for (CereBuyerUserFollow record : page1.getRecords()) {
            AttentionRes res = new AttentionRes();
            res.setUserId(record.getUserId());
            cereBuyerUsers.stream().findFirst().ifPresent(cereBuyerUser -> {
                res.setNickName(cereBuyerUser.getName());
                res.setHeadImage(cereBuyerUser.getHeadImage());
            });
            res.setStatus(followIdList.contains(record.getUserId()) ? 1 : 0);
            attentionRes.add(res);
        }


        return R.ok(attentionRes);
    }
}
